package com.jxc.action.xsgl;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.jxc.action.BaseAction;
import com.jxc.model.po.BasicArgument;
import com.jxc.model.vo.Json;
import com.jxc.model.vo.Print;
import com.jxc.model.vo.SessionInfo;
import com.jxc.model.vo.VSaleReturnlist;
import com.jxc.model.vo.VSalelist;
import com.jxc.model.vo.VSalelistItem;
import com.jxc.model.vo.VUser;
import com.jxc.service.BasicArgumentServiceI;
import com.jxc.service.SaleReturnlistServiceI;
import com.jxc.service.SalelistServiceI;
import com.jxc.service.UserServiceI;
import com.jxc.util.Constants;
import com.jxc.util.ExceptionUtil;
import com.jxc.util.ResourceUtil;
import com.jxc.util.SaleReturnInfo;
import com.opensymphony.xwork2.ModelDriven;
@Namespace("/xsgl")
@Action(value = "saleReturnlistAction", results = {@Result(name = "findSaleReturn", location = "/sale/findSaleReturn.jsp")})
public class SaleReturnlistAction extends BaseAction implements
		ModelDriven<VSaleReturnlist> {
	private final static Logger logger = Logger.getLogger(SaleReturnlistAction.class);

	@Autowired
	private SalelistServiceI salelistService;
	@Autowired
	private UserServiceI userService;
	@Autowired
	private BasicArgumentServiceI basicArgumentService;
	@Autowired
	private SaleReturnlistServiceI saleReturnlistService;

	private VSaleReturnlist model = new VSaleReturnlist();

	private String items;
	
	@Override
	public VSaleReturnlist getModel() {
		return model;
	}
	
	public String datagridSaleReturn(){
		return "findSaleReturn";
	}
	
	public String salereturnlist(){
		return items; 
	}
	
	public void datagrid(){
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest()
		.getSession().getAttribute(ResourceUtil.getSessionInfoName());
		if(sessionInfo.getRepertoryIds()!=null&&!sessionInfo.getRepertoryIds().equals("null")){
			model.setRid(Long.parseLong(sessionInfo.getRepertoryIds()));
			this.writeJson(saleReturnlistService.datagridReturn(model));
		}else{
			model.setSlid(sessionInfo.getUserId());
			this.writeJson(saleReturnlistService.datagridReturn(model));
		}
	}
	
	public void findSaleReturnDetail(){
		this.writeJson(saleReturnlistService.findSaleReturnDetail(model));
	}

	public void noAuth_searcheReturn(){
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest()
				.getSession().getAttribute(ResourceUtil.getSessionInfoName());
		VUser vuser = userService.get(sessionInfo.getUserId());
		if(vuser.getReid()!=null){
			model.setRid(vuser.getReid());
		}else{
			model.setUid(vuser.getUpid());
		}
		writeJson(saleReturnlistService.datagridSaleReturn(model));
	}
	
 

	public void noAuth_statusReturn() {
		String sid = getRequest().getParameter("sid");
		String ids = getRequest().getParameter("ids");
		VSalelist vSalelist = salelistService.getSalelistById(Long
				.parseLong(sid));
		BasicArgument argument = basicArgumentService.getBasricArgumentByKey(vSalelist.getRid(), Constants.INTEGRAL_CONVERSION_RATIO);
		List<VSalelistItem> vSalelistItems = salelistService.getItemsByIds(ids);
		VSaleReturnlist vSaleReturnlist = new VSaleReturnlist();
		Double amount = 0d;
		for (VSalelistItem item : vSalelistItems) {
			amount += item.getTotal();
		}
		if(vSalelist.getPointNum()!=null&&vSalelist.getPointNum()>0){
			vSaleReturnlist.setReturnPoint(vSalelist.getPointNum());
			amount -= Double.valueOf(vSalelist.getPointNum())/Integer.parseInt(argument.getArgValue());
		}else{
			vSaleReturnlist.setReturnPoint(0);
		}
		vSaleReturnlist.setRealReturn(amount);
		writeJson(vSaleReturnlist);
	}
	
	public void noAuth_add(){
		Json j = new Json();
		
		//获取数量进行比对
		
		//退货数量小于销售数量
		
		//每次退货后销售数量减去对应的数量
		 
		
			Print sign=saleReturnlistService.add(model, items);
			if(sign.getSign().equals(SaleReturnInfo.SUCCESS)){
				this.getSession().setAttribute("print", sign);
				j.setMsg("退货成功！");
				j.setSuccess(true);
				
			}else{
				j.setMsg("退货失败！");
				logger.info("退货失败");
			}
		
		super.writeJson(j);
	}
	public void noAuth_edit(){
		Json j = new Json();
		
			Print print = saleReturnlistService.saveOrUpdate(model, items);
			
			if(print.getSign().equals(SaleReturnInfo.SUCCESS)){
			
			this.getSession().setAttribute("print", print);
				j.setMsg("退货成功！");
				j.setSuccess(true);
			}else{
				j.setMsg("退货失败！");
				logger.info("退货失败");
			}
		
		super.writeJson(j);
	}
	
	public void noAuth_addBySrid(){
		Json j = new Json();
		
			Print print = saleReturnlistService.addBySrid(model, items);
			if(print.getSign().equals(SaleReturnInfo.SUCCESS)){
				this.getSession().setAttribute("print", print);
				j.setMsg("退货成功！");
				j.setSuccess(true);
			}else{
				j.setMsg("退货失败！");
			
				logger.info("退货失败");
			}
			
			
		super.writeJson(j);
	}
	
	public String getItems() {
		return items;
	}

	public void setItems(String items) {
		this.items = items;
	}

}
